package data;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

import logic.Admin;

public class JDBCAdminDAO implements IAdminDAO {

	public List<Admin> selectAllAdmins() {
		Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        List<Admin> searchResults = new LinkedList<Admin>();
        ResultSet result = null;

        try {
            String sql = "SELECT * FROM admin";
            stmt = conn.prepareStatement(sql);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                Admin temp = new Admin(result.getString("nick"));
                temp.setPass(result.getString("pass"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}

	public Admin select(Connection conn, String nick) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        Admin a = null;
        String sql = "SELECT * FROM admin WHERE nick = ? ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, nick);
            result = stmt.executeQuery();

            result.next();
            a = new Admin(result.getString("nick"));
            a.setPass(result.getString("pass"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return a;
	}

	public Admin login(Connection conn, String nick, String pass) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        Admin a = null;
        String sql = "SELECT * FROM admin WHERE nick = ? AND pass = ? ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, nick);
            stmt.setString(2, pass);
            result = stmt.executeQuery();

            result.next();
            if(result.isAfterLast()){
            	a = new Admin(result.getString("nick"));
            	a.setPass(result.getString("pass"));
            }else{
            	a = null;
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return a;
	}

}
